package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.GoodsBoxDetail;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.transmit.domain.DepotSkuDetail;
import com.lvy.hczn.transmit.mapper.DepotSkuDetailMapper;
import com.lvy.hczn.transmit.service.DepotSkuDetailService;
import com.lvy.hczn.transmit.service.TFrmLossService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@DS("slave")
public class DepotSkuDetailServiceImpl extends ServiceImpl<DepotSkuDetailMapper, DepotSkuDetail> implements DepotSkuDetailService {

    @Autowired
    private DepotSkuDetailService depotSkuDetailService;

    @Autowired
    private DepotSkuDetailMapper depotSkuDetailMapper;

    @Autowired
    private TFrmLossService tFrmLossService;

    /**
     * 查询仓库临期可报损商品
     *
     * @param paramInfo
     * @return
     * @author yw
     * @date 2023-08-31 20:53:15
     */
    @Override
    public List<DepotSkuDetail> selectByFrmLossByWarehouseCode(ParamInfo paramInfo) {
        /*if (paramInfo == null) {
            throw new UtilException("参数不能为空");
        }
        if (StrUtil.isEmpty(paramInfo.getWarehouseCode())) {
            throw new UtilException("仓库编码不能为空");
        }*/
        DepotSkuDetail depotSkuDetail = new DepotSkuDetail();
        if (paramInfo.getData() != null) {
            depotSkuDetail = JSON.parseObject(JSON.toJSONString(paramInfo.getData()), DepotSkuDetail.class);
        }
        depotSkuDetail.setWarehouseCode(paramInfo.getWarehouseCode());
        /**暂时未根据仓库循环查询，查询全部仓库的，再根据仓库做分发,每天凌晨定时调用，看效果再决定怎么处理**/
        List<DepotSkuDetail> list = depotSkuDetailMapper.selectByFrmLossByWarehouseCode(depotSkuDetail);
        if (!list.isEmpty()) {
            List<ParamInfo> paramInfoList = new ArrayList<>();
            LinkedHashMap<String, List<DepotSkuDetail>> depotSkuDetailMap = list.stream().collect(Collectors.groupingBy(DepotSkuDetail::getWarehouseCode, LinkedHashMap::new, Collectors.toList()));
            depotSkuDetailMap.forEach((key,value)->{
                ParamInfo info = new ParamInfo();
                info.setWarehouseCode(key);
                info.setList(value);
                paramInfoList.add(info);
            });
            tFrmLossService.syncFrmLossBatch(paramInfoList);
        }
        return list;
    }
}
